<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/30
 * Time: 16:17
 */
namespace app\appmsg\controller;

use app\common\helper\emotion_helper;
use app\common\helper\user_helper;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class M
{
    private $user_id;

    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
        $user_info = user_helper::get_user_info($this->user_id);
        if (!$user_info) {
            $this->user_id = 0;
        }
    }

    /**
     * 图文详情
     */
    public function __call($action = '', $param = array())
    {
        $task_id = Request::request('task_id', 0);
        $msg_id  = (int)$action;

        $info = _uri('appmsg', ['id'=>$msg_id]);

        if (!$info) {
            return redirect(url('/m/notfound/index'));
        }

        if ($info['relation_id']) {
            $info = _uri('appmsg',['id' => (int)$info['relation_id']]);
            if (!$info) {
                return redirect(url('/m/notfound/index'));
            }
        }

        // 如果是外链形式的图文，直接跳走
        if ($info['content_type'] == 1) {
            return redirect(url($info['link']));
        }

        // 分享任务需要有user_id
        if ($task_id && !$this->user_id) {
            //check_weixin_auth(AnUrl('appmsg/m/'.$msg_id.'?task_id='.$task_id));
        }

        //$detail_tpl_id = tpl_helper::get_detail_tpl();

        if (isset($info['title']) && $info['title']) {
            $info['title'] = emotion_helper::code_to_emoji($info['title']);
        }
        if (isset($info['content']) && $info['content']) {
            $info['content'] = emotion_helper::code_to_emoji($info['content']);
        }

        // 分享任务的阅读数加1
        if ($task_id) {
            //update_res_field('share_task', $task_id, 'SET `view_num`=`view_num`+1');
        }

        // 增加阅读量
        update_res_field('appmsg', $info['id'], 'view_num', 1);

        return View::fetch('detail', [
            'info'    => $info,
            'task_id' => $task_id
        ]);
        //Response::display(MODULE_PATH.'/m/template/detail_tpl/detail_'.$detail_tpl_id.'.html');
    }

    public function msg_send()
    {
        $msg_id = Request::request('msg_id', 0);
        $info = _uri('appmsg_send', $msg_id);
        if (!$info) {
            return redirect(url('/m/notfound/index'));
        }

        // 如果是外链形式的图文，直接跳走
        if ($info['content_type'] == 1) {
            return redirect(url($info['link']));
        }

        //$detail_tpl_id = tpl_helper::get_detail_tpl();

        if (isset($info['title']) && $info['title']) {
            $info['title'] = emotion_helper::code_to_emoji($info['title']);
        }
        if (isset($info['content']) && $info['content']) {
            $info['content'] = emotion_helper::code_to_emoji($info['content']);
        }

        View::fetch('detail',[
            'info' => $info
        ]);
    }
}